﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp2
{
    class Game
    {
        private const int GameWidth = 1000;
        private const int GameHeight = 600;


        private pacman _pacman;
        private money _money;
        private ghost _ghost;
        private List<Bar> Bars { get; set; }

        private Point[] _barPoints =
        {
             new Point(0,0),new Point(50,0),new Point(100,0),new Point(150,0), new Point(200,0),new Point(250,0), new Point(300,0),
            new Point(350,0),new Point(400,0), new Point(450,0),new Point(500,0),new Point(550,0),new Point(600,0),new Point(650,0),
            new Point(700,0),new Point(750,0),new Point(800,0),new Point(850,0),new Point(900,0),new Point(950,0),
            new Point(950,50),new Point(950,100),new Point(950,150),new Point(950,200),new Point(950,250),new Point(950,300),
            new Point(950,350),new Point(950,400),new Point(950,450),new Point(950,500),new Point(950,550),new Point(900,550),
            new Point(850,550),new Point(800,550),new Point(750,550),new Point(700,550),new Point(650,550),new Point(600,550),
            new Point(550,550),new Point(500,550),new Point(450,550),new Point(400,550),new Point(350,550),new Point(300,550),
            new Point(250,550),new Point(200,550),new Point(150,550),new Point(100,550),new Point(50,550),
            new Point(0,50),new Point(0,100),new Point(0,150),new Point(0,200),new Point(0,250),
             new Point(0,300),new Point(0,350),new Point(0,400),new Point(0,500),new Point(0,450),new Point(0,550),

            new Point(100,100),new Point(150,100), new Point(200,100), new Point(250,100), new Point(300,100), new Point(350,100),
             new Point(400,100), new Point(100,150), new Point(100,200), new Point(100,250), new Point(100,300), new Point(100,350),
              new Point(100,400), new Point(100,450), new Point(150,450), new Point(200,450), new Point(250,450), new Point(300,450),
               new Point(350,450), new Point(400,450),

            new Point(200,200),new Point(200,250),new Point(250,200),new Point(250,250),new Point(200,350),new Point(250,350),

            new Point(500,150),new Point(500,200),new Point(400,250),new Point(400,300),new Point(450,300),new Point(500,300),
            new Point(550,300),new Point(600,300),new Point(500,350),new Point(500,400),new Point(600,250),

            new Point(600,100),new Point(650,100),new Point(700,100),new Point(750,100),new Point(800,100),new Point(850,100),
            new Point(850,150),new Point(850,200),new Point(850,250),new Point(850,300),new Point(850,350),new Point(850,400),
            new Point(850,450),new Point(800,450),new Point(750,450),new Point(700,450),new Point(650,450),new Point(600,450),

            new Point(700,200),new Point(700,250),new Point(750,200),new Point(750,250),new Point(700,350),new Point(750,350),

        };
        private System.Timers.Timer Timer { get; set; }
        //定义委托
        public delegate void GameChanged();
        //定义事件
        public event GameChanged gameChanged;
        /// <summary>
        /// 初始化
        /// </summary>
        public Game()
        {
            _pacman = new pacman(new Rectangle(300, 300, 50, 50));
            _money = new money(new Rectangle(50, 50, 50, 50));
            _ghost = new ghost(new Rectangle(300, 400, 50, 50));
            //初始化墙
            Bars = new List<Bar>();
            foreach (var barPoints in _barPoints)
            {
                Bar bar = new Bar(new Rectangle(barPoints, new Size(50, 50)));
                Bars.Add(bar);
            }
            //初始化时钟
            Timer = new System.Timers.Timer();
            Timer.Elapsed += Timer_Elapsed;
            Timer.Start();
        }
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            _ghost.Move(new Size(GameWidth,GameHeight));
            gameChanged();
        }

        /// <summary>
        /// 绘图游戏场景
        /// </summary>
        /// <param name="g"></param>
        /// <param name="size"></param>
        public void Draw(Graphics g, Size size)
        {
            //定义空白的图
            Image img = new Bitmap(GameWidth, GameHeight);
            //画背景
            Graphics _g = Graphics.FromImage(img);
            Rectangle rec = new Rectangle(new Point(0, 0), new Size(GameWidth, GameHeight));

            _g.DrawImage(Properties.Resources.bg, rec);
            //画墙
            foreach (var bar in Bars)
            {
                bar.Draw(_g);
            }
            //画人
            _pacman.Draw(_g);
            //画金币
            _money.Draw(_g);
            //画幽灵
            _ghost.Draw(_g);
            //todo
            g.DrawImage(img, new Rectangle(new Point(0, 0), size));
        }
        /// <summary>
        /// 处理键盘
        /// </summary>
        /// <param name="key"></param>
        public void KeyDown(String key)
        {
            _pacman.KeyDown(key);
        }
    }
}
